package leetcode.d1;

import java.util.HashMap;

/**
 * @Author: hang an
 * @Date: 2025/5/25
 * @Description:
 */
public class R1557 {
    public int longestPalindrome(String[] words) {
        int res = 0;
        int s = 0;
        int ns = 0;
        boolean flag = false;
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            String reverseWord = word.charAt(1) + "" + word.charAt(0);
            Integer integer = map.get(reverseWord);
            if (integer != null && integer > 0) {
                flag = true;
            }
            if (word.equals(reverseWord)) {
                s++;
            }
            if (integer != null && integer > 0) {
                ns++;
                map.put(word, integer - 1);
            } else {
                map.put(word, map.getOrDefault(word, 0) + 1);
            }
        }
        res += 4 * ns;
        if (s!=0) res += 2;
        return res;
    }
}
